#! /usr/bin/perl

#package to hold a collection of subroutines that will be used throughout the project to validate certain user inputs
package ValidationSubs;

#enable strict mode
use strict;

#use database package
use DBQueries;

#subroutine to determine if a parameter-passed text is a valid file name
#this sub determines if a specific user already owns a file of that name
#this will return 1 for valid and 0 for invalid
sub validate_file_name{
	
	#obtain parameters
	#since this is a different module, the first argument will be the class
	#therefore call the second parameter
	my $username = $_[1];
	my $file_name = $_[2];
	
	#first ensure that name ends in .txt or .pdf and that it has a name
	if($file_name =~ m/.+\.txt$/ || $file_name =~ m/.+\.pdf$/){
		
		#now we knwo it is a text file
		#now call DB to see if taken
		if(DBQueries::isFile($username, $file_name) == 0){
		
			#return success
			return 1;
		}
		#otherwise the name is not availiable
		else{
			return 0;
		}
	}
	#otherwise teh name is not a .txt or pdf file
	else{
		return 0;
	}
}


#subroutine to determine if parameter-passed text is a valid group name
#this determimes if the group name is taken and if the length is larger than 0
#this will return 1 for valid and 0 for invalid
sub validate_group{
	
	#since this is a different module, the first argument will be the class
	#therefore call the second parameter
	my $name = $_[1];
	
	#first ensure that length is longer than 0
	if(length($name) > 0){
		
		#now make sure the name is not already taken
		if(DBQueries::isGroup($name) == 0){
			
			#in this case the group doesnt exist yet so return 1
			return 1;
		}
		#otherwise the group name is taken
		else{
			return 0;
		}
	}
	#otherwise the submission is false
	else{
		return 0;
	}
}

#subroutine to determine if parameter-passed text could be a name
#this will return 1 for valid and 0 for invalid
sub validate_name{
	
	#since this is a different module, the first argument will be the class
	#therefore call the second parameter
	my $name = $_[1];
	
	#since there are so many special names, just ensure that the string is at least one character long 
	#otherwise everythign should be accepted
	if(length($name) > 0){
		return 1;
	}
	#otherwise the submission is false
	else{
		return 0;
	}
}

#subroutine to determine if parameter-passed text could be a username
#this will return 1 for valid and 0 for invalid
sub validate_user{
	
	#since this is a different module, the first argument will be the class
	#therefore call the second parameter
	my $name = $_[1];
	
	#access DB to see if name is taken
	if(DBQueries::isUser($name) == 1){
		
		#return false
		return 0;
	}
	#otherwise name is valid
	else{
		return 1;
	}
	
}

#subroutine to determine if parameter-passed text could be an email address
#this will take addresses of the form example@example.com or example@example.co.uk
#this will return 1 for valid and 0 for invalid
sub validate_email{
	
	#since this is a different module, the first argument will be the class
	#therefore call the second parmeter
	my $email = $_[1];
	
	#now use regex to determine match
	if($email =~ m/^(.+)\@(.+)\.(..|...)\.?(..|...)?$/){
		return 1;
	}
	else{
		return 0;
	}
}

#subroutine to determine if two parameter passed passwords are identical 
#this is used to determine if the user re-typed his or her password correctly
sub validate_passwords{
	
	#since this is a different module, the first argument will be the class
	#therefore call the second and third parameter
	
	my $pword1 = $_[1];
	my $pword2 = $_[2];
	
	#first make sure they are not empty
	if(length($pword1) == 0 || length($pword2) == 0){
		return 0;
	}
	
	
	#now determine if equal
	if($pword1 eq $pword2){
		return 1;
	}
	else{
		return 0;
	}
}

#debug
sub validate_login{
	
	#obtain parameters
	#since this is a different module, the first argument will be the class
	#therefore call the second and third parameter
	my $username = $_[1];
	my $password = $_[2];
	
	#now call database to determine if the two match
	my $success = DBQueries::loginUser($username, $password);
	
	#return the value
	return $success;
}

#return 1 to ensure true value of module
1;